package com.by.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Doctorbean;
import com.by.yz.loginUtil.DiagnosisRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lp on 2017/4/13.
 */
public class diagnosis_doctor extends BaseActivity{

    RecyclerView recyclerView;
    SimpleStringRecyclerViewAdapter simpleStringRecyclerViewAdapter;
    List<Doctorbean> datas=new ArrayList<Doctorbean>();
    private static List<String> zxmName =new ArrayList<String>();
    private static List<String> zhengxiang = new ArrayList<String>();
    String bm;
    static String bingMing;
    LinearLayout Allbm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diagnosis_doctor);
        setToolbarTitle("选择医生");
        initView();
        initDatas();

    }

    private void initView() {
        Allbm = (LinearLayout)findViewById(R.id.allbm);
        bingMing = getIntent().getStringExtra("BingMing");
//        try {
//            bm = URLDecoder.decode(BingMing, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        try {
            bm = URLEncoder.encode(bingMing, "Utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            bingMing = URLEncoder.encode(bingMing, "Utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        DiagnosisRESTFul bmREST=new DiagnosisRESTFul();
        bmREST.bmDocterlist(bm,new DiagnosisRESTFul.MyGetCallBack(){
            @Override
            public void ok(JSONArray response) throws JSONException {
                if(response.length() == 0){
                    Allbm.setVisibility(View.GONE);
                }else{
                    Allbm.setVisibility(View.VISIBLE);
                }
                Log.e("hj",response.toString());
                Doctorbean doctorbean;
                for(int i=0;i<response.length();i++){
                    JSONObject jsonObject= response.getJSONObject(i);
                    doctorbean= ToJSonObject.jsonToBean(jsonObject.toString(),Doctorbean.class);
                    if(doctorbean.getFlag()) {
                        datas.add(doctorbean);
                    }
                }
                simpleStringRecyclerViewAdapter.notifyDataSetChanged();
            }

            @Override
            public void error(String error) {
                Log.e("hj","更新失败"+error);
            }
        });
    }

    private void initDatas() {
        recyclerView=(RecyclerView)findViewById(R.id.doctList);
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        simpleStringRecyclerViewAdapter=new SimpleStringRecyclerViewAdapter(diagnosis_doctor.this,datas);
        recyclerView.setAdapter(simpleStringRecyclerViewAdapter);
        recyclerView.addItemDecoration(new TechnologyTeamDividerItemDecoration(diagnosis_doctor.this,TechnologyTeamDividerItemDecoration.VERTICAL_LIST));
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private List<Doctorbean> mValues;

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public Doctorbean tt;
            public final View mView;
            public final NetworkImageView mImageView;
            public final TextView doctorName;
            public final TextView textHospital;
            public final TextView department;
            public final TextView instruc;
            public final TextView doctorPosition;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                mImageView = (NetworkImageView) view.findViewById(R.id.avatar);
                doctorName = (TextView) view.findViewById(R.id.doctorName);
                textHospital = (TextView) view.findViewById(R.id.textHospital);
                department = (TextView) view.findViewById(R.id.department);
                instruc =(TextView) view.findViewById(R.id.textSubDescripting);
                doctorPosition=(TextView)view.findViewById(R.id.doctorPosition);
            }
        }

        public Doctorbean getValueAt(int position) {
            return mValues.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<Doctorbean> items) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            mValues = items;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_page_list_item, parent, false);
            // view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {

            holder.tt = mValues.get(position);
            holder.doctorName.setText(mValues.get(position).getDoctorname());
            holder.textHospital.setText(mValues.get(position).getTextHospital().getHospital());
            holder.department.setText(mValues.get(position).getDepartment().getDepartment());
            holder.instruc.setText(mValues.get(position).getResume());
            holder.doctorPosition.setText(mValues.get(position).getPosition());
            holder.mImageView.setDefaultImageResId(R.mipmap.login_logo1);
            holder.mImageView.setErrorImageResId(R.mipmap.login_logo1);
            holder.mImageView.setImageUrl(mValues.get(position).getmImageView(), App.imageLoader);
            Log.e("haha","图片获取"+mValues.get(position).getmImageView());
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, diagnosis_zhengxiang.class);
                    Bundle bundle = new Bundle();
                    intent.putExtra("BingMing",bingMing);
                    intent.putExtra("DoctorName",holder.tt.getLogin());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
            Glide.with(holder.mImageView.getContext())
                    .load(mValues.get(position).getmImageView())
                    .fitCenter()
                    .into(holder.mImageView);
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }
}
